package com.vhans.bapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vhans.core.annotation.VisitLogger;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.model.dto.AgreementDTO;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.vo.agreement.AgreementCenterVO;
import com.vhans.db.model.vo.agreement.AgreementHomeListVO;
import com.vhans.db.model.vo.agreement.AgreementInfoVO;
import com.vhans.db.service.IBAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 约起控制起器
 *
 * @author vhans
 */
@Api(tags = "约起模块")
@RestController
public class AgreementController extends BaseController {

    @Autowired
    private IBAgreementService agreementService;

    /**
     * 查看约起列表
     *
     * @return {@link TableDataInfo}
     */
    @VisitLogger(value = "约起中心")
    @ApiOperation(value = "查看约起列表")
    @GetMapping("/agreement/list")
    public TableDataInfo listAgreementVO(Integer type) {
        startPage();
        List<AgreementHomeListVO> list = agreementService.listAgreementHomeVO(type);
        return getDataTable(list);
    }

    /**
     * 发表约起
     *
     * @param agreement 约起信息
     * @return {@link Result <>}
     */
    @SaCheckLogin
    @ApiOperation(value = "发表约起")
    @PostMapping("/agreement/add")
    public Result<?> addAgreement(@Validated @RequestBody AgreementDTO agreement) {
        agreementService.addAgreement(agreement, agreement.getAgreementType());
        return Result.success();
    }

    /**
     * 删除约起
     *
     * @param agreement 条件
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "删除约起")
    @DeleteMapping("/agreement/delete")
    public Result<?> deleteAgreement(@RequestBody AgreementDTO agreement) {
        agreementService.deleteAgreement(agreement);
        return Result.success();
    }

    /**
     * 修改约起
     *
     * @param agreement 约起信息
     * @return {@link Result<>}
     */
    @SaCheckLogin
    @ApiOperation(value = "修改约起")
    @PutMapping("/agreement/update")
    public Result<?> updateAgreement(@Validated @RequestBody AgreementDTO agreement) {
        agreementService.updateAgreement(agreement, agreement.getAgreementType());
        return Result.success();
    }

    /**
     * 编辑约起
     *
     * @param agreement   条件
     * @return {@link Result<AgreementInfoVO>} 约起info
     */
    @SaCheckLogin
    @ApiOperation(value = "编辑约起")
    @GetMapping("/agreement/edit")
    public Result<AgreementInfoVO> editAgreement(AgreementDTO agreement) {
        return Result.success(agreementService.editAgreement(agreement));
    }

    /**
     * 加入约起
     *
     * @param agreement   条件
     * @return {@link Result<AgreementInfoVO>} 约起info
     */
    @SaCheckLogin
    @ApiOperation(value = "加入约起")
    @PostMapping("/agreement/join")
    public Result<?> joinAgreement(@RequestBody AgreementDTO agreement) {
        return Result.success(agreementService.joinAgreement(agreement));
    }

    /**
     * 查看约起
     *
     * @param agreement 条件
     * @return {@link Result<AgreementCenterVO>} 约起
     */
    @VisitLogger(value = "约起中心")
    @ApiOperation(value = "查看约起")
    @GetMapping("/agreement/center")
    public Result<AgreementCenterVO> getAgreementAgreementById(AgreementDTO agreement) {
        return Result.success(agreementService.getAgreementAgreementById(agreement));
    }
}
